import sys
import time
import mysql.connector
import threading
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QApplication,QHBoxLayout,QLineEdit,QTextEdit,QDesktopWidget
from PyQt5.QtGui import QTextCursor
from PyQt5.QtCore import pyqtSignal, QObject
from changefunction import parse_next_url, getrate,get_itemids,getdriver
from crawler import downloaddata
import pymysql
from contextlib import contextmanager
import mysql.connector
from mysql.connector import pooling
print(mysql.connector.__version__)
import queue
from concurrent.futures import ThreadPoolExecutor
import threading
from followth_node import get_disk_serial_simple,getnodeorder,getnodescount
# from white_list_cache import WhiteListCache
a=234+23

print(f"a = {a}")
config = {
            "user": "root",
            "password":"Zb_200407",
            "host": "gz-cdb-d4j7h16x.sql.tencentcdb.com",
            "database": "eas",
            'port':22333,
            'connect_timeout': 10,
            'charset': 'utf8mb4',
            'pool_reset_session': True,
            'use_pure': True
        }
pool = mysql.connector.pooling.MySQLConnectionPool(
            pool_name='mypool',
            pool_size=10,
            **config
        )
# 添加这行来保持程序运行
# input("按回车键退出...")
def getconn(pool):
    conn = pool.get_connection()
    return conn
def getconn2(pool):
    conn = pool.get_connection()
    return conn
class MessageSender(QObject):
    signal = pyqtSignal(str)


class Window(QWidget):

    def __init__(self):
        super().__init__()

        layout_h = QHBoxLayout()
        
        self.resume_button = QPushButton("启动任务")
        self.resume_button.clicked.connect(self.resume_thread)
        self.pause_button = QPushButton("停止任务")
        self.pause_button.clicked.connect(self.pause_thread)
        self._pause_event = threading.Event()
        self._pause_event.clear()  #
        self.setGeometry(300, 300, 800, 500)
        self.setWindowTitle('关键词下载产品-RPA')
        text_layout = QHBoxLayout()
        self.textmemo = QTextEdit()
        text_layout.addWidget(self.textmemo)
        self.sender = MessageSender()
        self.sender.signal.connect(self.textmemo.append)
        layout_h.addWidget(self.resume_button)

        self.textmemo.append(
            '启动就可以下载关键词的对应数据')
        # layout_h.addWidget(self.pause_button)

        main_layout = QVBoxLayout()
        main_layout.addLayout(layout_h)
        main_layout.addLayout(text_layout)
        self.setLayout(main_layout)
        self.thread = []
        shoptype = ['ebay', 'ali']
        # shoptype = ['ali']

        conn = getconn(pool)
        # ebay_whitelist = white_list_cache.get_white_list(conn, 'ebay')
        # node_manager = NodeManager(conn)
        # if node_manager.update_node_status():
        #     print("Node status updated successfully")
        # else:
        #     print("Failed to update node status")
        # # for i,shoptype in enumerate(shoptype):
        #     y=get_itemids(conn,shoptype)
        #     print(y)
        # # self.drivers = getdriver()
        # desktop = QDesktopWidget()
        # width = desktop.screenGeometry().width()
        # height = desktop.screenGeometry().height()
        # self.drivers[0].set_window_position(0, int(height * 0.05))
        # self.drivers[0].set_window_size(width / 2, height)
        # # 设置第二个浏览器在右边
        # self.drivers[1].set_window_position(width / 2, int(height * 0.25))
        # self.drivers[1].set_window_size(width / 2, height)
        diskid=get_disk_serial_simple()
        tis = threading.Thread(target=downloaddata,args=(pool,diskid))
        self.thread.append(tis)
        
        self.resume_thread()
        # for index, sptype in enumerate(shoptype):
        #     driver = getdriver()
        #     lockdianxiaomi = threading.Lock()
        #     conn = getconn(pool)
        #     tis = threading.Thread(target=parse_next_url,
        #                            args=(conn,  sptype,driver,lockdianxiaomi))
        #     self.thread.append(tis)
        #     # tis.start()
    def pause_thread(self):
        # self._pause_event.clear()
        ixx = 2

    def resume_thread(self):
        for i in range(1):
            self.thread[i].start()
        self.resume_button.setEnabled(False)
        # self._pause_event.set()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())
